<template>
  <div @keyup.enter="getInvData()">
    <el-form class="query-table-form" :inline="true">
      <el-form-item>
        <el-input v-model="params.customer_name" placeholder="主体名称查询" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="params.corp_name" placeholder="负责人查询" :clearable="true"></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          clearable
          placeholder="地址查询"
          :auto-top="true"
          @change="getInvData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <c-select
          :options="enumStatus"
          v-model="selectStatus"
          clearable
          placeholder="立项状态"
          @change="handleStatusChange"
        ></c-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getInvData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-download"
          :loading="exportLoading"
          @click="exportExcel('项目调查（已通过）')"
        >
          导出
        </el-button>
      </el-form-item>
      <el-form-item class="float-right">
        <el-button @click="handleFlow">
          <i class="fa fa-sitemap"></i>
          审批流程
        </el-button>
      </el-form-item>
    </el-form>
    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <div class="text-truncate" :title="scope.row.address">{{ scope.row.address }}</div>
      </template>

      <template v-slot:name="scope">
        <el-popover placement="top-start" title="项目经理" trigger="hover">
          <div>
            <div>{{ `A角： ${scope.row.res_a_name || '-'}；` }}</div>
            <div class="mb-2">{{ `电话： ${scope.row.res_a_phone || '-'}` }}</div>
            <div>{{ `B角： ${scope.row.res_b_name || '-'}；` }}</div>
            <div>{{ `电话： ${scope.row.res_b_phone || '-'}` }}</div>
          </div>
          <div slot="reference">
            <div class="status-label badge-info text-left w-100">
              <div>A角:{{ scope.row.res_a_name }}</div>
              <div>B角:{{ scope.row.res_b_name }}</div>
              <div>风控员:{{ scope.row.res_person_risk_point }}</div>
            </div>
          </div>
        </el-popover>
      </template>
      <template v-slot:itemStatus="scope">
        {{ scope.row.itemStatusLabel }}
      </template>
    </c-table>
    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getInvData"
      ></el-pagination>
    </div>

    <view-dialog :visible.sync="viewVisible" :related-data="viewRow"></view-dialog>
    <flow-dialog
      title="项目调查审批流程"
      :visible.sync="flowVisible"
      :flow-key="flowKey"
    ></flow-dialog>
  </div>
</template>

<script>
import { enumGuatype, enumProvinceEleStatus, enumProcessStatus } from '@/credit/plugins/enumTown'
import { baseTableColumns } from '@/credit/views/county/unionProvince/Approve'
import baseMixins from '@/credit/views/city/self/normal/mixins'

import SelectAddress from '@/components/select/SelectAddress'
import ViewDialog from '@/credit/views/city/self/normal/invest/ViewDialog'
import { getFollow } from '@/credit/mixin/taskTableCountyUnionProvince'
import { wfKey } from '@/credit/views/city/self/components/investApprove/index'
import FlowDialog from '@/credit/components/flow/ViewDialog'
export default {
  components: {
    SelectAddress,
    ViewDialog,
    FlowDialog,
  },
  mixins: [baseMixins],
  data() {
    return {
      enumGuatype,
      enumProvinceEleStatus,
      order: {
        N1confuse_status: 'asc',
        N2project_res_status: 'asc',
        N3item_date: 'desc',
      },
      params: {
        index: 1,
        size: 15,
        address_code: '',
        customer_name: '',
        corp_name: '',
        'item_status.eq': 1,
        subKey: 'project_approval_register.customer_related_id',
      },

      tableData: [],
      tableColumns: [
        ...baseTableColumns,
        // { attrs: { label: '建议金额(万元)', prop: 'approved_funds', align: 'center' } },
        { attrs: { label: '审批开始时间', prop: 'res_date', width: 100 } },
        { attrs: { label: '审批金额(万元)', prop: 'approveFunds', align: 'center' } },
        {
          attrs: { label: '审批完成时间', prop: 'city_res_date_corp', width: 140 },
          process: v => v || '-',
        },
        {
          attrs: { label: '合作银行', prop: 'bank_name', align: 'center' },
          process: v => v || '-',
        },
        // { attrs: { label: '审批模式', prop: 'processLabel', width: 80, align: 'center' } },
        { attrs: { label: '项目经理', prop: 'resName', width: 100 }, slot: 'name' },
        {
          attrs: { label: '待审核人员', prop: 'itemStatusLabel', align: 'center' },
          slot: 'itemStatus',
        },
        {
          attrs: { label: '操作', align: 'center', width: 80 },
          operations: [{ content: '查看', icon: 'fa fa-search', event: this.handleView }],
        },
      ],

      viewVisible: false,
      viewRow: {},

      enumStatus: [
        { label: '待调查', value: 0 },
        { label: '审批中', value: 1 },
        { label: '已审批', value: 2 },
        { label: '已拒绝', value: 3 },
      ],
      enumParams: {
        0: {
          'confuse_status.neq': 3,
          'project_res_status.eq': 0,
        },
        1: {
          'confuse_status.neq': 3,
          'project_res_status.eq': 1,
        },
        2: {
          'confuse_status.neq': 3,
          'project_res_status.eq': 2,
        },
        3: {
          'confuse_status.eq': 3,
          'contract_approval_status.lt': 1,
        },
      },
      selectStatus: '',
      flowVisible: false,
      flowKey: wfKey,
    }
  },
  computed: {},
  watch: {},
  mounted() {},
  created() {
    this.getInvData()
  },
  methods: {
    handleFlow() {
      // let flowPage = this.$router.resolve({ path: '/flowkey', query: { key: wfKey } })
      // window.open(flowPage.href, '_blank')
      this.flowVisible = true
    },
    async getInvData(index = 1) {
      await this.getData(index)
      const record = await getFollow(this.tableData, 'PROJECT_APPROVAL_DIR')
      this.tableData = this.tableData.map(it => {
        const data = record.find(item => item.wf_related_id === it.customer_related_id) || {}
        it.itemStatusLabel = data.innerFollow || ''
        return it
      })
    },
    dataAdapter(item) {
      item.guaType = enumGuatype[item.gua_type]
      item.res_date_corp = this.$utils.dateFormat(item.res_date_corp, this.dateFmt)
      item.resName = `A角: ${item.res_a_name} B角: ${item.res_b_name}`
      const projectApproval = item.project_approval_register || []
      const approveRes =
        projectApproval.find(item => +item.approval_register_type === 2) ||
        (projectApproval?.[0] ?? {})
      item.approveFunds = approveRes.res_funds || '-'
      item.processLabel = enumProcessStatus[+item.process_status] || ''

      // const itemStatus = enumProjectResStatus[item.project_res_status] || {}
      item.itemStatusLabel = ''
      // item.itemStatusStyle = itemStatus.css

      return item
    },
    handleView(row) {
      this.viewRow = row
      this.viewVisible = true
    },
    handleStatusChange() {
      const stautsParams = this.enumParams[this.selectStatus] || {}
      const resetParams = Object.keys(this.enumParams).reduce((prev, curr) => {
        for (const key in this.enumParams[curr]) {
          prev[key] = ''
        }
        return prev
      }, {})
      this.params = {
        ...this.params,
        ...resetParams,
        ...stautsParams,
      }
      this.getInvData()
    },
  },
}
</script>
<style lang="scss" scoped>
.status-label {
  display: inline-block;
  padding: 0.25em 0.4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  vertical-align: baseline;
  border-radius: 0.25rem;
  transition: color 0.15s;
}
</style>
